package cn.crex1.xiaomi.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static cn.crex1.xiaomi.common.ProductEnum.CLIST;
import static cn.crex1.xiaomi.common.ProductEnum.PLIST;
import static cn.crex1.xiaomi.common.ProductEnum.CPLIST;
import cn.crex1.xiaomi.common.ResponseCode;
import cn.crex1.xiaomi.common.ServerResponse;
import cn.crex1.xiaomi.dao.CategorySDao;
import cn.crex1.xiaomi.dao.ProductDao;
import cn.crex1.xiaomi.dao.impl.CategorySDaoImpl;
import cn.crex1.xiaomi.dao.impl.ProductDaoImpl;
import cn.crex1.xiaomi.pojo.Category;
import cn.crex1.xiaomi.pojo.Product;
import cn.crex1.xiaomi.pojo.SAttribute;
import cn.crex1.xiaomi.pojo.Specification;
import cn.crex1.xiaomi.service.ProductService;
import cn.crex1.xiaomi.util.JsonUtils;
import cn.crex1.xiaomi.util.PagesUtil;
import cn.crex1.xiaomi.util.RedisUtil;
import cn.crex1.xiaomi.util.StringUtil;
import cn.crex1.xiaomi.util.TimeUtil;
import cn.crex1.xiaomi.vo.ProductVO;

public class ProductServiceImpl implements ProductService {

	private ProductDao productDao = new ProductDaoImpl();

	Product product = new Product();
	SAttribute sAttribute = new SAttribute();
	Specification specification = new Specification();

	@SuppressWarnings("unchecked")
	@Override
	public ServerResponse<List<Product>> selectAllproduct(Integer interval) {
		//查询缓存plist
		if (RedisUtil.Keys.exists(PLIST.getDesc())) {
			if (RedisUtil.Keys.ttl(PLIST.getDesc()) > 0) {
				//获取产品数据
				List<Product> c_plist = (List<Product>) JsonUtils.readObjectByString(
						RedisUtil.Strings.get(PLIST.getDesc()), 
						JsonUtils.getCollectionType(List.class, Product.class));
				//定时器取值
				return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(),
						TimeUtil.timerLists(c_plist, interval));
			}
		} else {
			//数据库查询产品
			List<Product> pList = productDao.selectAllProduct(product);
			if (pList.size() > 0) {
				//缓存
				RedisUtil.Strings.set(PLIST.getDesc(), JsonUtils.writeStringByObject(pList));
				//过期时间
				RedisUtil.Keys.expired(PLIST.getDesc(), 300);
				if (RedisUtil.Keys.ttl(PLIST.getDesc()) > 0) {
					//取回数据
					return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(),
							TimeUtil.timerLists(pList, interval));
				}
			}
		}
		return ServerResponse.createByErrorMessage(ResponseCode.ERROR.getDesc());
	}

	@Override
	public String insertAllOsproduct(Product product) {
		List goods = productDao.selectAllProduct(product);
		int insert = productDao.insertAllOsproduct(product);
		if (goods.equals(insert)) {
			return "已有商品，添加失败";
		}
		return String.valueOf(insert);
	}

	@Override
	public String deleteOsproductByProductid(Product product) {
		return null;
	}

	@Override
	public ServerResponse<List<Category>> selectAllProductcategory(Integer type) {
		Category category = new Category();
		category.setType(type);
		//查询缓存 缓存过期时间
		if (RedisUtil.Keys.exists(CLIST.getDesc())) {
			if (RedisUtil.Keys.ttl(CLIST.getDesc()) > 0) {
				//取出分类列表
				List<Category> c_clist = JsonUtils.readObjectByString(RedisUtil.Strings.get(CLIST.getDesc()),
						JsonUtils.getCollectionType(List.class, Category.class));
				//返回分类数据
				return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(), c_clist);
			}
		} else {
			//从数据库查询分类
			List<Category> clist = productDao.selectAllProductcategory(category);
			if (clist.size() > 0) {
				//存入缓存
				RedisUtil.Strings.set(CLIST.getDesc(), JsonUtils.writeStringByObject(clist));
				RedisUtil.Keys.expired(CLIST.getDesc(), 600);
				//设置过期
				if (RedisUtil.Keys.ttl(CLIST.getDesc()) > 0) {
					return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(), clist);
				}
			}
		}
		return ServerResponse.createByErrorMessage(ResponseCode.ERROR.getDesc());
	}

	@Override
	public List<Product> selectOsproductByDetails(long product_number) {
		product.setProduct_number(product_number);
		List<Product> details = productDao.selectOsproductByDetails(product, sAttribute, specification);
		if (details != null) {
			return details;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ServerResponse<PagesUtil> selectOProductByOCategoryId(ProductVO productVO) {
		// 查询某个分类下的商品
		Integer category_id = productVO.getCategory_id();
		if (RedisUtil.Keys.exists(CPLIST.getDesc() + category_id)) {
			List<ProductVO> cplist = JsonUtils.readObjectByString(RedisUtil.Strings.get(CPLIST.getDesc() + category_id),
					JsonUtils.getCollectionType(List.class, ProductVO.class));
			//分页 
			PagesUtil pages = PagesUtil.pageDatasCache(cplist,productVO.getPerPageDataCount());
			
			return ServerResponse
					.createBySuccess(pages);
		} else if (RedisUtil.Keys.exists(CPLIST.getDesc())) {
			// 查询指定分类商品
			List<String> list = JsonUtils.findValuesbyJsonString(RedisUtil.Strings.get(CPLIST.getDesc()), "category_id",
					StringUtil.stringValue(category_id));
			if (list != null) {
				// 保存每个分类的商品
				RedisUtil.Strings.set(CPLIST.getDesc() + category_id, list.toString());
				RedisUtil.Keys.expired(CPLIST.getDesc() + category_id, 300);

				List<ProductVO> cplist = JsonUtils.readObjectByString(list.toString(),
						JsonUtils.getCollectionType(List.class, ProductVO.class));
				
				//分页 
				PagesUtil pages = PagesUtil.pageDatasCache(cplist,productVO.getPerPageDataCount());
				
				return ServerResponse.createBySuccess(pages);
			}
		} else {
			// 查询所有分类商品
			List<ProductVO> products = productDao.selectAllCategoryProduct(productVO);
			if (products != null) {
				// 保存所有分类商品
				RedisUtil.Strings.set(CPLIST.getDesc(), JsonUtils.writeStringByObject(products));
				RedisUtil.Keys.expired(CPLIST.getDesc(), 600);
				// 查询指定分类商品
				List<String> list = JsonUtils.findValuesbyJsonString(RedisUtil.Strings.get(CPLIST.getDesc()),
						"category_id", StringUtil.stringValue(category_id));
				if (list != null) {
					List<ProductVO> cplist = JsonUtils.readObjectByString(list.toString(),
							JsonUtils.getCollectionType(List.class, ProductVO.class));
					//分页
					PagesUtil pages = PagesUtil.pageDatasCache(cplist,productVO.getPerPageDataCount());
					
					return ServerResponse.createBySuccess(pages);
				}
			}
		}
		return ServerResponse.createByErrorMessage(ResponseCode.ERROR.getDesc());
	}
}
